<?php

/*
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {

	private $data;
	private $error;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Login_model');
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			//code...
			exit();
		}
		$this->data 	= array();
		$this->error 	= array();
	}

	public function get_bound_types($stock_type, $bound_type_flg=FALSE)
	{
		$this->load->model('Inbound_Type_model');
		$this->load->model('Outbound_Type_model');
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();

		if ('inbound' == $stock_type)
		{
			if ($bound_type_flg)
			{
				if ('all'==$bound_type_flg)
				{
					$this->data['ajax']['request'] = 'DONE';
					$this->data['ajax']['data'] = array();
				}
				else if ($this->Inbound_Type_model->get_by_flg($bound_type_flg))
				{
					$this->data['ajax']['request'] = 'DONE';
					$this->data['ajax']['data'] = $this->Inbound_Type_model->inbound_types('ID_NAME');
				}
				else
				{
					$this->data['ajax']['request'] = 'FAILED';
					$this->data['ajax']['data'] = '获得指定入库类型失败';
				}
			}
			else
			{
				$this->data['ajax']['request'] = 'DONE';
				$this->data['ajax']['data'] = $this->Inbound_Type_model->inbound_flgs('ID_NAME');
			}
		}
		else if ('outbound' == $stock_type)
		{
			if ($bound_type_flg)
			{
				if ('all'==$bound_type_flg)
				{
					$this->data['ajax']['request'] = 'DONE';
					$this->data['ajax']['data'] = array();
				}
				else if ($this->outbound_Type_model->get_by_flg($bound_type_flg))
				{
					$this->data['ajax']['request'] = 'DONE';
					$this->data['ajax']['data'] = $this->Outbound_Type_model->outbound_types('ID_NAME');
				}
				else
				{
					$this->data['ajax']['request'] = 'FAILED';
					$this->data['ajax']['data'] = '获得指定出库类型失败';
				}

			}
			else
			{
				$this->data['ajax']['request'] = 'DONE';
				$this->data['ajax']['data'] = $this->Outbound_Type_model->outbound_flgs('ID_NAME');
			}
		}
		else
		{
			$this->data['ajax']['request'] = 'DONE';
			$this->data['ajax']['data'] = array();
		}

		$this->load->view('ajax_json', $this->data);
	}

	//快速搜索，第一步搜索主货物，第二步查询子货物库存详情
	public function quick_search()
	{
		//载入必要的模型
		$this->load->model('Main_Goods_model');
		$this->load->model('Sub_goods_model');

		//初始化参数
		$response	= FALSE;
		$data 		= array();
		$view 		= FALSE;
		$goods_type = $this->input->post('goods_type');
		$q 			= $this->input->post('q');

		if ('mg' == $goods_type)	//mg : main_goods
		{
			$view = 'ajax/quick_search_content_view';
			//字符长度达到2开始查询
			if (strlen($q) > 1)
			{
				$response = 'DONE';
				$data = $this->Main_Goods_model->search('s', $q);
			}
			else
			{
				$response = 'FAILED';
			}
		}
		else if ('sg' == $goods_type)		//sg : sub_goods
		{
			//设置视图
			$view = 'ajax/popup_goods_stock_view';
			//获得对应主货物的所有子货物
			$response = 'DONE';
			$data = $this->Sub_goods_model->get_goods_by('main_goods_id', $q, 'attribute, stock, stock_unkown');
		}
		else
		{
			$response = 'FAILED';
		}

		//检查数据是否为空或FALSE
		if (!$data || empty($data))
		{
			$response = FALSE;
			$data = array();
		}

		$this->data['request'] = $response;
		$this->data['data'] = $data;
		$this->load->view($view, $this->data);
	}

}